package com.flong.selenium;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

/***
 *@Author:liangjilong
 *@Date:2016年1月15日下午3:01:25
 *@Email:jilongliang@sina.com
 *@Version:1.0
 *@CopyRight(c)liangjilong
 *@Description:
 */
public class SouGouImages {
	public static ChromeDriver driver;
	public static int count=0;
	public static int count1=0;
	public static String urlreq ="";
    static {
        Logger logger = Logger.getLogger("com.gargoylesoftware.htmlunit");
        logger.setLevel(Level.OFF);
        System.setProperty("webdriver.chrome.driver", "D:\\DevSoft\\chromedriver.exe");
    	driver = new ChromeDriver();
    	driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//SECONDS是秒的意思
    	 //刷新页面请求
    	//driver.navigate().refresh();
    	//driver.findElement(By.className("add")).click();//找到这个按钮点击一下获取下一页
    	count =0;
    	count1 =0;
    }

    public static void main(String[] args) throws IOException, Exception {
    	 
    	urlreq ="http://pic.sogou.com/pics?query=美女&p=40230500&st=255&mode=255";
    	
    	// urlreq=URLEncoder.encode(urlreq, "utf-8");
    	
    	urlreq = URLDecoder.decode(urlreq, "utf-8");
    	driver.get(urlreq);
    	
    	for (int i = 1; i <=5; i++) {
    		
    		WebElement picLists = driver.findElementById("imgid").findElement(By.id("picList1"));
    		List<WebElement> uls=picLists.findElements(By.tagName("ul"));
        	
        	for(WebElement ul:uls){
        		count ++;
        		List<WebElement> lis = ul.findElements(By.tagName("li"));
        			
        		for (WebElement li : lis) {
        			
        			List<WebElement> imgs = li.findElements(By.tagName("img"));
        			
        			for (WebElement img : imgs) {
        				
        				if(img!=null){
        					String srcHref =img.findElement(By.className("img-hover")).getAttribute("src");
                			
                			System.out.println(srcHref); 
        				}
        				
            			
            			count1++;
            			
        			}
        			
    			}
        		
        	}
		}
    	
    	 System.out.println("共："+count+"----");
    	 System.out.println("共："+count1+"----");
    	 
    	
    	
    	//driver.FindElement(By.XPath("//input[@id='submit' and @value='下一步']")).click()；
    	//数据加载完之后，自动关闭浏览器.
    	//driver.close();
    	//driver.quit();
    	
    }

}
